home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2002 November / CD 1 / APC0211D1.ISO / workshop / prog / files / ActivePerl-5.6.1.633-MSWin32.msi / _af8eb328ac3302319cf0fc5254d9c89a < prev    next >
Encoding:
Text File  |  2001-10-17  |  8.2 KB  |  269 lines

  1. package URI::file;
  2.  
  3. use strict;
  4. use vars qw(@ISA);
  5.  
  6. require URI::_generic;
  7. @ISA = qw(URI::_generic);
  8.  
  9. # Map from $^O values to implementation classes.  The Unix
  10. # class is the default.
  11. my %os_class = (
  12.      os2     => "OS2",
  13.      mac     => "Mac",
  14.      MacOS   => "Mac",
  15.      MSWin32 => "Win32",
  16.      win32   => "Win32",
  17.      msdos   => "FAT",
  18.      dos     => "FAT",
  19.      qnx     => "QNX",
  20. );
  21.  
  22. sub os_class
  23. {
  24.     my($OS) = shift || $^O;
  25.  
  26.     my $class = "URI::file::" . ($os_class{$OS} || "Unix");
  27.     no strict 'refs';
  28.     unless (%{"$class\::"}) {
  29.     eval "require $class";
  30.     die $@ if $@;
  31.     }
  32.     $class;
  33. }
  34.  
  35. sub path { shift->path_query(@_) }
  36. sub host { shift->authority(@_)  }
  37.  
  38. sub new
  39. {
  40.     my($class, $path, $os) = @_;
  41.     os_class($os)->new($path);
  42. }
  43.  
  44. sub new_abs
  45. {
  46.     my $class = shift;
  47.     my $file = $class->new(shift);
  48.     return $file->abs($class->cwd) unless $$file =~ /^file:/;
  49.     $file;
  50. }
  51.  
  52. sub cwd
  53. {
  54.     my $class = shift;
  55.     require Cwd;
  56.     my $cwd = Cwd::cwd();
  57.     $cwd = VMS::Filespec::unixpath($cwd) if $^O eq 'VMS';
  58.     $cwd = $class->new($cwd);
  59.     $cwd .= "/" unless substr($cwd, -1, 1) eq "/";
  60.     $cwd;
  61. }
  62.  
  63. sub file
  64. {
  65.     my($self, $os) = @_;
  66.     os_class($os)->file($self);
  67. }
  68.  
  69. sub dir
  70. {
  71.     my($self, $os) = @_;
  72.     os_class($os)->dir($self);
  73. }
  74.  
  75. 1;
  76.  
  77. __END__
  78.  
  79. =head1 NAME
  80.  
  81. URI::file - URI that map to local file names
  82.  
  83. =head1 SYNOPSIS
  84.  
  85.  use URI::file;
  86.  
  87.  $u1 = URI->new("file:/foo/bar");
  88.  $u2 = URI->new("foo/bar", "file");
  89.  
  90.  $u3 = URI::file->new($path);
  91.  $u4 = URI::file->new("c:\\windows\\", "win32");
  92.  
  93.  $u1->file;
  94.  $u1->file("mac");
  95.  
  96. =head1 DESCRIPTION
  97.  
  98. The C<URI::file> class supports C<URI> objects belonging to the I<file>
  99. URI scheme.  This scheme allows us to map the conventional file names
  100. found on various computer systems to the URI name space.  An old
  101. specification of the I<file> URI scheme is found in RFC 1738.  Some
  102. older background information is also in RFC 1630. There are no newer
  103. specifications as far as I know.
  104.  
  105. If you want simply to construct I<file> URI objects from URI strings,
  106. use the normal C<URI> constructor.  If you want to construct I<file>
  107. URI objects from the actual file names used by various systems, then
  108. use one of the following C<URI::file> constructors:
  109.  
  110. =over 4
  111.  
  112. =item $u = URI::file->new( $filename, [$os] )
  113.  
  114. Maps a file name to the I<file:> URI name space, creates an URI object
  115. and returns it.  The $filename is interpreted as one belonging to the
  116. indicated operating system ($os), which defaults to the value of the
  117. $^O variable.  The $filename can be either absolute or relative, and
  118. the corresponding type of URI object for $os is returned.
  119.  
  120. =item $u = URI::file->new_abs( $filename, [$os] )
  121.  
  122. Same as URI::file->new, but will make sure that the URI returned
  123. represents an absolute file name.  If the $filename argument is
  124. relative, then the name is resolved relative to the current directory,
  125. i.e. this constructor is really the same as:
  126.  
  127.   URI::file->new($filename)->abs(URI::file->cwd);
  128.  
  129. =item $u = URI::file->cwd
  130.  
  131. Returns a I<file> URI that represents the current working directory.
  132. See L<Cwd>.
  133.  
  134. =back
  135.  
  136. The following methods are supported for I<file> URI (in addition to
  137. the common and generic methods described in L<URI>):
  138.  
  139. =over 4
  140.  
  141. =item $u->file( [$os] )
  142.  
  143. This method return a file name.  It maps from the URI name space
  144. to the file name space of the indicated operating system.
  145.  
  146. It might return C<undef> if the name can not be represented in the
  147. indicated file system.
  148.  
  149. =item $u->dir( [$os] )
  150.  
  151. Some systems use a different form for names of directories than for plain
  152. files.  Use this method if you know you want to use the name for
  153. a directory.
  154.  
  155. =back
  156.  
  157. The C<URI::file> module can be used to map generic file names to names
  158. suitable for the current system.  As such, it can work as a nice
  159. replacement for the C<File::Spec> module.  For instance the following
  160. code will translate the Unix style file name F<Foo/Bar.pm> to a name
  161. suitable for the local system.
  162.  
  163.   $file = URI::file->new("Foo/Bar.pm", "unix")->file;
  164.   die "Can't map filename Foo/Bar.pm for $^O" unless defined $file;
  165.   open(FILE, $file) || die "Can't open '$file': $!";
  166.   # do something with FILE
  167.  
  168. =head1 MAPPING NOTES
  169.  
  170. Most computer systems today have hierarchically organized file systems.
  171. Mapping the names used in these systems to the generic URI syntax
  172. allows us to work with relative file URIs that behave as they should
  173. when resolved using the generic algorithm for URIs (specified in RFC
  174. 2396).  Mapping a file name to the generic URI syntax involves mapping
  175. the path separator character to "/" and encoding of any reserved
  176. characters that appear in the path segments of the file names.  If
  177. path segments consisting of the strings "." or ".." have a
  178. different meaning than what is specified for generic URIs, then these
  179. must be encoded as well.
  180.  
  181. If the file system has device, volume or drive specifications as
  182. the root of the name space, then it makes sense to map them to the
  183. authority field of the generic URI syntax.  This makes sure that
  184. relative URI can not be resolved "above" them , i.e. generally how
  185. relative file names work in those systems.
  186.  
  187. Another common use of the authority field is to encode the host that
  188. this file name is valid on.  The host name "localhost" is special and
  189. generally have the same meaning as an missing or empty authority
  190. field.  This use will be in conflict with using it as a device
  191. specification, but can often be resolved for device specifications
  192. having characters not legal in plain host names.
  193.  
  194. File name to URI mapping in normally not one-to-one.  There are
  195. usually many URI that map to the same file name.  For instance an
  196. authority of "localhost" maps the same as a URI with a missing or empty
  197. authority.
  198.  
  199. Example 1: The Mac use ":" as path separator, but not in the same way
  200. as generic URI. ":foo" is a relative name.  "foo:bar" is an absolute
  201. name.  Also path segments can contain the "/" character as well as be
  202. literal "." or "..".  It means that we will map like this:
  203.  
  204.   Mac                   URI
  205.   ----------            -------------------
  206.   :foo:bar     <==>     foo/bar
  207.   :            <==>     ./
  208.   ::foo:bar    <==>     ../foo/bar
  209.   :::          <==>     ../../
  210.   foo:bar      <==>     file:/foo/bar
  211.   foo:bar:     <==>     file:/foo/bar/
  212.   ..           <==>     %2E%2E
  213.   <undef>      <==      /
  214.   foo/         <==      file:/foo%2F
  215.   ./foo.txt    <==      file:/.%2Ffoo.txt
  216.   
  217. Note that if you want a relative URL, you *must* begin the path with a :.  Any
  218. path that begins with [^:] will be treated as absolute.
  219.  
  220. Example 2: The Unix file system is easy to map as it use the same path
  221. separator as URIs, have a single root, and segments of "." and ".."
  222. have the same meaning.  URIs that have the character "\0" or "/" as
  223. part of any path segment can not be turned into valid Unix file names.
  224.  
  225.   Unix                  URI
  226.   ----------            ------------------
  227.   foo/bar      <==>     foo/bar
  228.   /foo/bar     <==>     file:/foo/bar
  229.   /foo/bar     <==      file://localhost/foo/bar
  230.   file:         ==>     ./file:
  231.   <undef>      <==      file:/fo%00/bar
  232.   /            <==>     file:/
  233.  
  234. =cut
  235.  
  236.  
  237. RFC 1630
  238.  
  239.    [...]
  240.  
  241.    There is clearly a danger of confusion that a link made to a local
  242.    file should be followed by someone on a different system, with
  243.    unexpected and possibly harmful results.  Therefore, the convention
  244.    is that even a "file" URL is provided with a host part.  This allows
  245.    a client on another system to know that it cannot access the file
  246.    system, or perhaps to use some other local mechanism to access the
  247.    file.
  248.  
  249.    The special value "localhost" is used in the host field to indicate
  250.    that the filename should really be used on whatever host one is.
  251.    This for example allows links to be made to files which are
  252.    distribted on many machines, or to "your unix local password file"
  253.    subject of course to consistency across the users of the data.
  254.  
  255.    A void host field is equivalent to "localhost".
  256.  
  257. =head1 SEE ALSO
  258.  
  259. L<URI>, L<File::Spec>, L<perlport>
  260.  
  261. =head1 COPYRIGHT
  262.  
  263. Copyright 1995-1998 Gisle Aas.
  264.  
  265. This library is free software; you can redistribute it and/or
  266. modify it under the same terms as Perl itself.
  267.  
  268. =cut
  269.